package com.firefighting.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.firefighting.dao.FirstMaintenanceProjectMapper;
import com.firefighting.dao.SecondMaintenanceProjectMapper;
import com.firefighting.entity.FirstMaintenanceProject;
import com.firefighting.entity.SecondMaintenanceProject;

@Service
public class MaintenanceService {

	@Autowired
	FirstMaintenanceProjectMapper firstMaintenanceProjectMapper;
	@Autowired
	SecondMaintenanceProjectMapper secondMaintenanceProjectMapper;
	/**
	 * @author Liuxc
	 * @function 查询所有一级维保项目目录
	 * */
	public  List<FirstMaintenanceProject> getAllFirstMaintenance() {
		// TODO Auto-generated method stub
		
		return firstMaintenanceProjectMapper.getAllFirstMaintenance();
	}
	/**
	 * @author Liuxc
	 * 将目录转化为jsonArray格式
	 * @param secondMaintenanceList */
	public JSONArray getMaintenanceTree(List<FirstMaintenanceProject> firstMaintenance, List<Integer> secondMaintenanceListInContract) {
		// TODO Auto-generated method stub
		JSONArray jsonArray=new JSONArray();
		
		for(FirstMaintenanceProject item : firstMaintenance){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", item.getId());
			jsonObject.put("text",item.getName());
			JSONObject state = new JSONObject();
			state.put("checked", false);//一级目录默认状态未选中
			jsonObject.put("state", state);
			jsonArray.add(jsonObject);
		}
		jsonArray = getCompleteTree(jsonArray,secondMaintenanceListInContract);

		return jsonArray;
	}
	
	/**
	 * @author Liuxc
	 * 将二级目录转化为jsonArray格式
	 * @param secondMaintenanceListInContract */
	public JSONArray getSecondMenu(List<SecondMaintenanceProject> secondtMaintenance, List<Integer> secondMaintenanceListInContract) {
		// TODO Auto-generated method stub
		JSONArray jsonArray=new JSONArray();
		
		for(SecondMaintenanceProject item : secondtMaintenance){
			JSONObject state = new JSONObject();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("id", item.getId());
			jsonObject.put("text",item.getName());
			jsonObject.put("father", item.getFatherId());
			if(secondMaintenanceListInContract.contains(item.getId())){//给二级目录添加状态（是否被选中）
				
				state.put("checked", true);
			}else{
				state.put("checked", false);
			}
			jsonObject.put("state", state);
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}
    /**@author Liuxc
     * @param secondMaintenanceList 
     * 
     * @function 将二级目录加到对应的一级目录下
     *@return JSONarray
     * **/
	public JSONArray getCompleteTree(JSONArray jsonArray, List<Integer> secondMaintenanceListInContract){
		
		for(int i = 0 ; i < jsonArray.size() ; i++){
			JSONObject jsonObject = jsonArray.getJSONObject(i);//一级目录元素
			Boolean flag =  true;
			List<SecondMaintenanceProject> secondMaintenanceProjectList = new ArrayList<SecondMaintenanceProject>();
			secondMaintenanceProjectList = secondMaintenanceProjectMapper.getByParentId(jsonObject.get("id"));//获取该一级目录下所有的二级目录
			for(SecondMaintenanceProject item: secondMaintenanceProjectList){
				if(!secondMaintenanceListInContract.contains(item.getId())){//如果二级目录有被选中的，修改状态
					flag = false;
					break;
				}
			}
			JSONObject state = new JSONObject();
			state.put("checked", flag);
			jsonObject.put("state", state);
			jsonObject.put("nodes", getSecondMenu(secondMaintenanceProjectList,secondMaintenanceListInContract));//添加子节点
			jsonObject.remove("id");
		}
		
		return jsonArray;
	}
}
